/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.supplier;

import org.hutech09.jsale.web.manager.model.LazySupplierDataModel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hutech09.jsale.bsn.ISupplierManager;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Supplier;

/**
 *
 * @author Nam
 */
@ManagedBean
@SessionScoped
public class SupplierBrowseCtrl {

    private static final Logger LOG = Logger.getLogger(SupplierBrowseCtrl.class.getName());

    @EJB
    private ISupplierManager supplierManager;

    private LazySupplierDataModel dataModel;

    private Supplier selected;

    @PostConstruct
    public void init() {
        dataModel = new LazySupplierDataModel(supplierManager);
    }

    public LazySupplierDataModel getDataModel() {
        return dataModel;
    }

    public void setDataModel(LazySupplierDataModel dataModel) {
        this.dataModel = dataModel;
    }

    public Supplier getSelected() {
        if(selected==null) 
            return new Supplier();
        return selected;
    }

    public void setSelected(Supplier selected) {
        System.out.println("Selected: " + selected);
        this.selected = selected;
    }

    public void delete() {
        if (selected == null) {
            return;
        }
        try {
            supplierManager.delete(selected.getId());
            LOG.info("Deleted: " + selected);
            selected = null;
        } catch (ItemNotFoundException ex) {
            Logger.getLogger(SupplierBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String update() {
        if (selected != null) {
            try {
                supplierManager.update(selected);
            } catch (ItemNotFoundException ex) {
                Logger.getLogger(SupplierBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return "supplierPage";
    }
}
